package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.SystemMenu;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ISystemMenuService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import cn.wolfcode.crm.util.SystemMenuUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @program: crm
 * @description:
 * @author: Mr.chen
 * @create: 2018-07-07 22:06
 **/
@Controller
@RequestMapping("systemmenu")
public class SystemMenuController {
    @Autowired
    private ISystemMenuService service;

    @RequestMapping("")
    @PermissionName("系统菜单主页")
    @RequiresPermissions("systemmenu:index")
    public String index() {
        return "systemmenu/list";
    }

    @RequestMapping("list")
    @ResponseBody
    @RequiresPermissions("systemmenu:list")
    @PermissionName("系统菜单数据")
    public PageResult list(QueryObject qo) {
        PageResult result = service.query(qo);
        return result;
    }

    @RequestMapping("listRoot")
    @ResponseBody
    public List<SystemMenu> listRoot() {
        /*List<SystemMenu> list = service.listRoot();
        //菜单权限过滤
        SystemMenuUtil.filterMenu(list);*/
        return (List<SystemMenu>) SecurityUtils.getSubject()
                .getSession().getAttribute(SystemMenuUtil.SYSTEM_MENU_IN_SESSION);
    }


    @RequestMapping("save")
    @ResponseBody
    @RequiresPermissions("systemmenu:save")
    @PermissionName("系统菜单保存")
    public JsonResult save(SystemMenu entity) {
        JsonResult result = new JsonResult();
        try {
            service.save(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult("保存操作失败");
        }
        return result;
    }

    @RequestMapping("update")
    @ResponseBody
    @RequiresPermissions("systemmenu:update")
    @PermissionName("系统菜单更新")
    public JsonResult update(SystemMenu entity) {
        JsonResult result = new JsonResult();
        try {
            service.update(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult("更新操作失败");
        }
        return result;
    }

    @RequestMapping("delete")
    @ResponseBody
    @RequiresPermissions("systemmenu:delete")
    @PermissionName("菜单删除")
    public JsonResult delete(Long id) {
        JsonResult result = new JsonResult();
        try {
            service.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("删除操作失败");
        }
        return result;
    }
    
}
